package com.demo2do.fms.admin.service.supplier.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.SupplierProfileDao;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.supplier.trans.CapacityType;
import com.demo2do.fms.entity.supplier.trans.PropertyType;
import com.demo2do.fms.entity.supplier.trans.TrailerType;
import com.demo2do.fms.entity.supplier.trans.TransSupplierVehicleStat;
import com.demo2do.fms.entity.supplier.trans.VehicleType;

/**
 * 
 * @author Wy
 *
 */
@Service("transSupplierVehicleStatService")
public class TransSupplierVehicleStatServiceImpl extends DataServiceImpl<TransSupplierVehicleStat> implements TransSupplierVehicleStatService{

    @Dao
    private SupplierProfileDao transportVehicleInfoDao;

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#listTransSupplierVehicleStat(java.lang.Long)
     */
    public List<TransSupplierVehicleStat> listTransSupplierVehicleStat(Long supplierId) {
        return transportVehicleInfoDao.listTransportVehicle(supplierId);
    }

   /*
    * (non-Javadoc)
    * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#listTransSupplierVehicleStat(java.lang.Long, com.demo2do.fms.entity.supplier.trans.VehicleType, com.demo2do.fms.entity.supplier.trans.TrailerType)
    */
    public List<TransSupplierVehicleStat> listTransSupplierVehicleStat(Long supplierId, VehicleType vehicleType, TrailerType trailerType) {
        
        if(trailerType != null) {
            
            return transportVehicleInfoDao.listTransportVehicle(supplierId, vehicleType, trailerType);
            
        } else{
            
            return transportVehicleInfoDao.listTransportVehicle(supplierId, vehicleType);
            
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#getTransSupplierVehicleStat(java.lang.Long, com.demo2do.fms.entity.supplier.trans.VehicleType, com.demo2do.fms.entity.supplier.trans.TrailerType)
     */
    public TransSupplierVehicleStat getTransSupplierVehicleStat(Long supplierId, VehicleType vehicleType, TrailerType trailerType) {
        return transportVehicleInfoDao.getTransSupplierVehicleStat(supplierId, vehicleType, trailerType);
    }


   /*
    * (non-Javadoc)
    * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#create(java.lang.Long, java.util.List)
    */
    public void create(Long supplierId, List<Long> values) {
        
        Supplier supplier = genericDaoSupport.load(Supplier.class, supplierId);
        
        for (int i = 0; i < values.size(); i++) {
            TransSupplierVehicleStat transSupplierVehicleStat = new TransSupplierVehicleStat(supplier, PropertyType.values()[i], values.get(i));
            genericDaoSupport.save(transSupplierVehicleStat.create());
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#createTrailer(java.lang.Long, com.demo2do.fms.entity.supplier.trans.TransSupplierVehicleStat, com.demo2do.fms.admin.entity.security.Login)
     */
    public void createTrailer(Long supplierId, TransSupplierVehicleStat stat, Login login) {
        
        Date now = new Date();

        // 1. load supplier
        Supplier supplier = genericDaoSupport.load(Supplier.class, supplierId);
        
        // 2. save stat to database
        genericDaoSupport.save(stat.create(supplier, login.getId(), now));

    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#edit(java.util.List, java.util.List)
     */
    public void edit(List<Long> transSupplierVehicleStatIds,List<Long> amounts) {
         
        for(int i=0;i<transSupplierVehicleStatIds.size();i++){
            
            TransSupplierVehicleStat transSupplierVehicleStat = genericDaoSupport.load(TransSupplierVehicleStat.class,transSupplierVehicleStatIds.get(i));
            
            if((StringUtils.equalsAnyIgnoreCase(transSupplierVehicleStat.getVehicleType().getAlias(), "car")) && (StringUtils.equalsAnyIgnoreCase(transSupplierVehicleStat.getTrailerType().getCode(), "one"))){
                
                genericDaoSupport.update(transSupplierVehicleStat.edit(CapacityType.values()[i].getCode(), amounts.get(i)));
            }else{
                
                genericDaoSupport.update(transSupplierVehicleStat.edit(amounts.get(i)));
            }
            
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#edit(com.demo2do.fms.entity.supplier.trans.TransSupplierVehicleStat, com.demo2do.fms.admin.entity.security.Login)
     */
    public void edit(TransSupplierVehicleStat stat, Login login) {
        genericDaoSupport.update(stat.operator(new Date(), login.getId()));
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TransSupplierVehicleStatService#create(java.lang.Long, java.util.List, java.util.List)
     */
    public void create(Long supplierId, List<TrailerType> trailerTypes, List<Long> amounts) {
        
        Supplier supplier = genericDaoSupport.load(Supplier.class, supplierId);
        
        for (int i = 0; i < amounts.size(); i++) {
            TransSupplierVehicleStat transSupplierVehicleStat = new TransSupplierVehicleStat(supplier, trailerTypes.get(i), amounts.get(i));
            genericDaoSupport.save(transSupplierVehicleStat.create());
        }
        
    }

}
